import java.util.ArrayList;
import java.util.List;

public class Order {
    private String orderId;
    private Customer customer;
    private List<OrderItem> itemList;
    private double totalAmount;

    public Order(String orderId, Customer customer) {
        this.orderId = orderId;
        this.customer = customer;
        this.itemList = new ArrayList<>();
        this.totalAmount = 0.0;
    }

    public void addDish(Dish dish, int quantity) throws DishOffShelfException, InsufficientStockException {
        if (!dish.isOnShelf()) {
            throw new DishOffShelfException("菜品《" + dish.getName() + "》已下架，无法点餐");
        }
        dish.reduceStock(quantity);
        itemList.add(new OrderItem(dish, quantity));
        totalAmount += dish.getPrice() * quantity;
    }

    public double calculateTotal() {
        return totalAmount;
    }

    public void printOrderInfo() {
        System.out.println("=== 订单ID：" + orderId + " ===");
        System.out.println("顾客：" + customer.getCustomerId() + " | 电话：" + customer.getPhone() + " | 地址：" + customer.getAddress());
        System.out.println("--- 菜品列表 ---");
        for (OrderItem item : itemList) {
            Dish dish = item.getDish();
            System.out.println(dish.getName() + " | 数量：" + item.getQuantity() + " | 单价：" + dish.getPrice());
        }
        System.out.println("--- 订单总价：" + calculateTotal() + " 元 ---");
    }
}